package com.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.model.User;
import com.example.utils.RestResult;
import com.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;


@RestController
@RequestMapping(value = "user", produces = MediaType.APPLICATION_JSON_VALUE)
public class UserController {

    @Autowired
    UserService userService;

    @RequestMapping(value = "info", method = RequestMethod.GET)
    public RestResult info() {

        User user = userService.getById(1);
        return RestResult.ok(user);
    }
    @RequestMapping(value = "getById", method = RequestMethod.GET)
    public RestResult getById(@RequestParam String id) {

        User user = userService.getById(id);
        return RestResult.ok(user);
    }

    @RequestMapping(value = "list", method = RequestMethod.GET)
    public RestResult list() {
        List<User> list = userService.list();

        return RestResult.ok(list);
    }

    @RequestMapping(value = "listMap", method = RequestMethod.GET)
    public RestResult listMap() {
        List<Map<String, Object>> list = userService.listMaps();

        return RestResult.ok(list);
    }

    @RequestMapping(value = "listMap1", method = RequestMethod.POST)
    public RestResult listMap1() {

        List<Map<String, Object>> list = userService.listMaps1();

        return RestResult.ok(list);
    }

    @RequestMapping(value = "listMap2", method = RequestMethod.GET)
    public RestResult listMap2() {
        List<Map<String, Object>> list = userService.listMaps1();
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("name", "email", "manager_id", "create_time");
        List<Map<String, Object>> li = userService.listMaps(queryWrapper);

        return RestResult.ok(li);
    }
}
